package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.SignCalc;
import cn.wolfcode.trip.base.domain.SignDetail;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.ISignCalcService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("signs")
@RestController
public class SignClacController {

    @Autowired
    private ISignCalcService iSignCalcService;
    @Autowired
    private IUserService userService;

    @GetMapping("/{id}")
    public Object listSign(@PathVariable Long id, String yearAndMoth) {
        //查询出签到的日期格式设置为[{"signDay":"10"}]
        List<String> day = iSignCalcService.selectListDay(id,yearAndMoth);
        List<Map<String, Object>> signDays = new ArrayList<>();
        for (String s : day) {
             Map<String, Object> signDay = new HashMap<>();
             signDay.put("signDay",s);
             signDays.add(signDay);
        }

        return signDays;
    }
    @PostMapping("/signDay")
    public JsonResult signDay(Long userId){
        JsonResult jsonResult = new JsonResult();
        //首次签到添加表格
        Boolean aBoolean = iSignCalcService.queryFirst(userId);
        if (aBoolean==false) {
            iSignCalcService.save(userId);
        }
        //查询今天是否有签到
        SignCalc todaySign = iSignCalcService.todaySign(userId);
        //查询出该用户的积分进行累加
        SignDetail signDetailGrade = iSignCalcService.queryGrade(userId);
        //查询出连续签到的次数
        Integer succRecordDays = iSignCalcService.querySuccRecord(userId);
        if (succRecordDays>=7) {
            Integer days=0;
            iSignCalcService.updateSuccRecord(userId,days);
            userService.updateState(userId,User.STATE_ENABLE);
        }
        System.out.println("积分:"+signDetailGrade);
        System.out.println(todaySign);
        if (todaySign==null) {//今天未签到 1:查询本月昨日签到情况
        SignCalc yesterdaySign = iSignCalcService.queryYesterdayByUserId(userId);
        //查询出昨天的累计签到的次数
            SignCalc signCalcGet = iSignCalcService.getSignRecord(userId);
            if (yesterdaySign==null) {
            //昨天未签到那么连续签到天数设置为1,并保存
                SignCalc signCalc = new SignCalc();
                signCalc.setUserId(userId);
                signCalc.setSuccRecord(1);
                signCalc.setSignData(new Date());
                iSignCalcService.addSign(signCalc);
                //判断是否是第一次签到并获取积分
                if (signDetailGrade==null) {
                //并且添加积分10点
                SignDetail signDetail = new SignDetail();
                signDetail.setUserId(userId);
                signDetail.setGrade(10L);
                iSignCalcService.addGrade(signDetail);
                return jsonResult;
                }else{
                    //已经有积分,修改积分
                    SignDetail signDetail = new SignDetail();
                    signDetail.setUserId(userId);
                    signDetail.setGrade(signDetailGrade.getGrade()+10L);
                    iSignCalcService.updateGrade(signDetail);
                    return jsonResult;
                }
            }else{//昨日已签到
                SignCalc signCalc = new SignCalc();
                signCalc.setUserId(userId);
                signCalc.setSuccRecord(signCalcGet.getSuccRecord()+1);
                signCalc.setSignData(new Date());
                iSignCalcService.addSign(signCalc);
                //累加积分
                SignDetail signDetail = new SignDetail();
                signDetail.setUserId(userId);
                signDetail.setGrade(signDetailGrade.getGrade()+10L);
                iSignCalcService.updateGrade(signDetail);
                return jsonResult;
            }

        }else{//当日已经签到
            return jsonResult.mark("已经签到过了");
        }
    }
    //获取3天5天7天积分接口
    @PutMapping("/{id}")
    public JsonResult drawIntegral(@PathVariable Long id){
        //判断前台传输领取按钮的参数
        //先查询出当天的连续签到数
        JsonResult jsonResult = new JsonResult();
        Integer succRecordDays = iSignCalcService.querySuccRecord(id);
        System.out.println(succRecordDays);
        SignDetail signDetailGrade = iSignCalcService.queryGrade(id);
        if (succRecordDays!=null&&succRecordDays>=3&&User.STATE_ENABLE==0) {
            SignDetail signDetail = new SignDetail();
            signDetail.setUserId(id);
            signDetail.setGrade(signDetailGrade.getGrade()+50L);
            iSignCalcService.updateGrade(signDetail);
            //如果领取设置一个状态给前端
            userService.updateState(id,User.STATE_DISABLE);
            //查询出状态给页面设值
            User user = userService.selectUserById(id);
            jsonResult.setResult(user);
           return jsonResult;
        }else{
            User user = userService.selectUserById(id);
            jsonResult.setResult(user);
            jsonResult.mark("领取失败");
            return jsonResult;
        }

    }
    //获取state类型
    @GetMapping("/state/{id}")
    public Object queryState(@PathVariable Long id){
        //查询出状态值
        User user = userService.selectUserById(id);
        //查询出连续签到的天数是否为7
        Integer succRecordDays = iSignCalcService.querySuccRecord(id);
        List<Map<Object, Object>> signDays = new ArrayList<>();
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("result",user);
        hashMap.put("signNum",succRecordDays);
        signDays.add(hashMap);
        return signDays;
    }

    /**
     * 根据用户id查询该用户的总积分
     */
    @GetMapping("/{userId}/grade")
    public SignDetail queryGrade(@PathVariable Long userId){
        return iSignCalcService.queryGrade(userId);
    }

    @GetMapping("/{userId}/isSign")
    public boolean isSign(@PathVariable Long userId){
        return iSignCalcService.todaySign(userId)!=null;
    }
}
